import os
import cPickle as pickle

class ObjDB:
    def __init__(self, filename):
        node_path = os.path.abspath(os.path.dirname(__file__))
        self.filename = os.path.join(node_path, filename)
        self.objdb = {}

    def __getitem__(self, key):
        return self.objdb.get(key)

    def __setitem__(self, key, value):
        self.objdb[key] = value

    def __delitem__(self, key):
        del self.objdb[key]

    def __len__(self):
        return len(self.objdb)

    def __contains__(self, key):
        return key in self.objdb

    def __iter__(self):
        return self.objdb.iterkeys()

    def load(self):
        if os.path.exists(self.filename):
            f = open(self.filename, 'r')
            self.objdb = pickle.loads(f.read())

    def save(self):
        f = open(self.filename, 'w')
        s = pickle.dumps(self.objdb)
        f.write(s)

